<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\User;
use app\admin\model\OperationTask as OperationTaskModer;
use app\admin\model\Shop;
use app\admin\model\DistributeTask;
use app\admin\model\ShopPriceLog;
use think\Db;
use think\Exception;

class OperationTask extends Api
{
    protected $NeedLogin = []; //存在则不验证token

    public function _initialize(){
	    $action = request()->action();
		if(!in_array($action,$this->NeedLogin)){
			$ignore = array('host','accept','content-length','content-type');
			$headers = array();
			if($this->request->request('token')){
				$headers['token'] = $this->request->request('token');
			}else{
				foreach($_SERVER as $key=>$value){
					if(substr($key, 0, 5)==='HTTP_'){
						$key = substr($key, 5);
						$key = str_replace('_', ' ', $key);
						$key = str_replace(' ', '-', $key);
						$key = strtolower($key);
						if(!in_array($key, $ignore)){
							$headers[$key] = $value;
						}
					}
				}
			}
			$token=$headers['token'];
			if(strlen($token)!=32){
				$this->error('请登录');
			}
			$this->user=User::where(array('token'=>$token))->find();
			if(!$this->user){
				$this->error('请登录');
			}
            if($this->user['status'] != 1){
                $this->error('账号异常');
            }

			User::where(array('id'=>$this->user['id']))->update(array('active_time'=>time()));
		}
	}

   
    public function getOperationTaskList(){
        $type = $this->request->request('type','');
        $page = $this->request->request('page',1);
        $pagesize = $this->request->request('pagesize',10);
        $list = OperationTaskModer::where(['type'=>$type,'status'=>1])->order('add_time desc')->limit(($page-1)*$pagesize,$pagesize)->select();
        foreach ($list as &$v) {
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            $v['imgs'] = explode(',',$v['imgs']);
            $v['imgs'] = $v['imgs'][0];
        }
        $this->success('操作成功',$list);
    }

    public function getOperationTaskInfo(){
        $type = $this->request->request('type','');
        $id = $this->request->request('id','');
        $info = OperationTaskModer::where(['id'=>$id,'type'=>$type])->find();
        if(!$info){
            $this->error('参数错误');
        }
        $info['imgs_arr'] = explode(',',$info['imgs']);
        $this->success('操作成功',$info);
    }


	
	public function add(){
        $data['friendship'] = $this->request->request('friendship','');
        $data['task_explain'] = $this->request->request('task_explain','');
        $data['material_img'] = $this->request->request('material_img','');
        $data['issue_type'] = $this->request->request('issue_type','');
		$data['add_time'] = time();

		if(empty($data['issue_type'])){
			$this->error('请选择发布类型');
		}
		if(empty($data['task_explain'])){
			$this->error('任务说明不可为空');
		}
		if(empty($data['friendship'])){
			$this->error('参考笔记不可为空');
		}
		$shop = Shop::where(['user_id'=>$this->user['id']])->find();
        if(!$shop){
            $this->error('操作错误');
        }
		$balance = 25;
		if($data['issue_type'] == 2){
			$balance = 20;
		}elseif($data['issue_type'] == 3){
			$balance = 15;
		}
		if($shop['balance'] < $balance){
			$this->error('账号余额不足');
		}

		$arr = [
			'title'=>$data['friendship'],
			'content'=>$data['task_explain'],
			'imgs'=> $data['material_img'],
			'task_type'=>5,
			'type'=>$shop['name'],
			'category'=>$shop['name'],
			'character_type'=>1,
			'status'=>-3,
			'shop_id'=>$shop['id'],
			'add_time'=>time()
		];

		$id = DistributeTask::insertGetId($arr);

		$data['task_id'] = $id;
		$data['type'] = $arr['type'];
		OperationTaskModer::insert($data);
		//冻结商户金额
		$arr = [
			'user_id'=>$shop['user_id'],
			'shop_id'=>$shop['id'],
			'task_id'=>$id,
			'is_freeze'=>1,
			'price'=>$balance*-1,
			'type'=>1,
			'add_time'=>time(),
			'consumption_type'=>1
		];
		
		ShopPriceLog::insert($arr);
		//扣除用户账号金额
		$data = ['balance'=>$shop['balance']-$balance];
		Shop::where(['id'=>$shop['id']])->update($data);
		//商户资金变动日志
		add_shop_balance_log($shop['id'],$balance,$shop['balance'],1); 
		$this->success('操作成功');

    }
}